package com.xoms.admin.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.PurchaseBean;
import com.xoms.admin.model.PurchaseDetailBean;
import com.xoms.admin.model.vo.AuditPurechaseBean;
import com.xoms.admin.model.vo.BatchPurchaseBean;
import com.xoms.admin.persist.dao.PurchaseDao;
import com.xoms.util.StringUtil;

@Repository
public class PurchaseImpl extends BaseImpl implements PurchaseDao {
	
	final String NAME_SPACE = NAME_SPACE_HEADER + ".PurchaseMapper";

	@Override
	public int selectCount(String key, String storeHouseId, String startTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if (!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if (!StringUtil.isEmpty(storeHouseId)) {
			params.put("storeHouseId", storeHouseId);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", params);

	}

	@Override
	public List<PurchaseBean> selectList(String key, int page, int pageSize, String storeHouseId, String startTime,
			String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if (!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if (!StringUtil.isEmpty(storeHouseId)) {
			params.put("storeHouseId", storeHouseId);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);

	}

	@Override
	public PurchaseBean selectOneById(String pid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", pid);
	}

	@Override
	public int selectDetailCount(String key, String purchaseId) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(purchaseId)) {
			params.put("purchaseId", purchaseId);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDetailCount", params);
	
	}

	@Override
	public List<PurchaseDetailBean> selectDetailList(String key, int page, int pageSize, String purchaseId, int paging) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(purchaseId)) {
			params.put("purchaseId", purchaseId);
		}
		params.put("paging", paging);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDetailList", params);

	}

	@Override
	public String selectMaxPureNoInYear() {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectMaxPureNoInYear");
	}

	@Override
	public int insert(PurchaseBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(PurchaseBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}

	@Override
	public int del(String purchaseId) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete", purchaseId);
	}

	@Override
	public int updateSubmitStatus(String purchaseId, Integer status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pid", purchaseId);
		params.put("status", status);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateSubmitStatus", params);
	}

	@Override
	public int replaceBatchDetail(List<BatchPurchaseBean> batch) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", batch);
		return sqlSessionTemplate.insert(NAME_SPACE + ".replaceBatchDetail", params);
	}

	@Override
	public int delDetails(String purchaseId) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delDetails", purchaseId);
	}

	@Override
	public int selectDetailCountById(String purchaseId) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDetailCountById", purchaseId);
	}

	@Override
	public int updateTotalCost(String purchaseId, double totalCost) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pid", purchaseId);
		params.put("totalCost", totalCost);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateTotalCost", params);
	}

	@Override
	public AuditPurechaseBean selectAuditPureDetailsById(String aid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectAuditPureDetailsById", aid);
	}

}
